package com.person.base.action;

import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.person.base.model.BaseRole;
import com.person.base.service.BaseRoleService;
import com.person.commons.struts2.BaseAction;
import com.person.sys.model.SysFunc;
import com.person.sys.model.SysMenu;
import com.person.sys.service.SysMenuService;

public class BaseRoleAction extends BaseAction {
	
	private BaseRoleService baseRoleService;
	private SysMenuService sysMenuService;
	private BaseRole baseRole;
	private List<BaseRole> roles;
	private List<SysMenu> sysMenus;
	private String[] func;
	private String[] role;

	/**
	 * 显示所有的角色列表	
	 * @return
	 */
	public String index(){
		
		roles = baseRoleService.roleList();
		
		return "index";
	}
	
	/**
	 * 转向到添加角色的页面
	 * @return
	 */
	public String preCreate(){
		
		return "createRole";
	}
	
	/**
	 * 添加角色
	 * @return
	 */
	public String create(){
		
		char status = baseRoleService.createRole(baseRole);
		if(status == '1'){
			info = getText("role.create.success");
		}else if(status == '0'){
			info = getText("role")+baseRole.getRoleName()+getText("role.has.in");
		}
		return "create_success";
	}
	
	/**
	 * 转向到修改页面
	 * @return
	 */
	public String preModify(){
		
		baseRole = baseRoleService.findByRoleId(baseRole.getRoleId());
		return "modifyRole";
	}
	
	/**
	 * 修改角色
	 * @return
	 */
	public String modify(){
		
		char status = baseRoleService.modifyRole(baseRole);
		if(status == '1'){
			info = getText("role.modify.success");
		}else if(status == '0'){
			info = getText("role")+baseRole.getRoleName()+getText("role.has.in");
		}
		return index();
	}
	
	/**
	 * 管理角色的权限
	 * 转向到页面
	 * 增加 删除 修改权限
	 * @return
	 */
	public String preManagePermission(){
		roles = baseRoleService.roleList();
		
		sysMenus = sysMenuService.juniorSysMenuList();
		
		
		return "managePermission";
	}
	
	public String managePermission(){
		String funcIdGroup = "";
		if(func!= null && func.length>0){
			for(int j=0;j<func.length;j++){
				funcIdGroup += func[j]+",";
			}
			
			if(role != null && role.length>0){
				for(int i=0;i<role.length;i++){
					String roleId = role[i];
					baseRole = baseRoleService.findByRoleId(roleId);
					baseRole.setFuncIdGroup(funcIdGroup);
				}
				
				info = getText("role.modify.success");
			}
		}
		
		return "managePermission_suc";
	}
	
	/**
	 * 管理某个角色的权限
	 * 增加 删除 修改权限
	 * @return
	 */
	public String preModifyPermission(){
		
		sysMenus = sysMenuService.juniorSysMenuList();
		
		baseRole = baseRoleService.findByRoleId(baseRole.getRoleId());
		
		String funcIdGroup = baseRole.getFuncIdGroup();
		
		if(StringUtils.isNotBlank(funcIdGroup)){
			
			funcIdGroup = funcIdGroup.substring(0,funcIdGroup.length()-1);
			
			func = funcIdGroup.split(",");
		}
		
		return "modifyPermission";
	}
	
	public String modifyPermission(){
		
		String funcIdGroup = "";
		if(null!=func&&func.length>0){
			for(int j=0;j<func.length;j++){
				funcIdGroup += func[j]+",";
			}
		}
		baseRole = baseRoleService.findByRoleId(baseRole.getRoleId());
		
		baseRole.setFuncIdGroup(funcIdGroup);
		
		info = getText("role.modify.success");
		return "modifyPermission_succ";
	}
	
	
	
	/**
	 * 转向到调整角色
	 * @return
	 */
	public String preModifyOrder(){
		
		roles = baseRoleService.roleList();
		return "modifyOder";
	}
	
	/**
	 * 调整角色序号高低
	 * @return
	 */
	public String modifyOder(){
		
		return "modifyOrder_success";
	}
	
	/**
	 * 删除角色
	 * @return
	 */
	public String delete(){
		char status = baseRoleService.deleteRole(baseRole);
		
		if(status == '1'){
			info = getText("role.delete.success");
		}
		return index();
	}
	
	public BaseRole getBaseRole() {
		return baseRole;
	}

	public void setBaseRole(BaseRole baseRole) {
		this.baseRole = baseRole;
	}

	public List<BaseRole> getRoles() {
		return roles;
	}

	public void setRoles(List<BaseRole> roles) {
		this.roles = roles;
	}

	public void setBaseRoleService(BaseRoleService baseRoleService) {
		this.baseRoleService = baseRoleService;
	}

	public void setSysMenuService(SysMenuService sysMenuService) {
		this.sysMenuService = sysMenuService;
	}

	public List<SysMenu> getSysMenus() {
		return sysMenus;
	}

	public void setSysMenus(List<SysMenu> sysMenus) {
		this.sysMenus = sysMenus;
	}

	public String[] getFunc() {
		return func;
	}

	public void setFunc(String[] func) {
		this.func = func;
	}

	public String[] getRole() {
		return role;
	}

	public void setRole(String[] role) {
		this.role = role;
	}
	
	
}
